package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;

import net.wsfcp.android.BuildConfig;
import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqRegisterBean;
import net.wsfcp.android.bean.resultbean.UserBean;
import net.wsfcp.android.http.UrlHelper;
import net.wsfcp.android.http.exception.ApiException;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;
import net.wsfcp.android.utils.EncryptUtils;

import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Shaoboyang on 2018/12/14.
 * 主要做一些数据处理呀,网路请求
 */

public class RegisterModel extends BaseModel {

    public void register(@NonNull String username, @NonNull String pwd, @NonNull String mail, @NonNull String code, @NonNull final RegisterInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");


        String secretPwd = EncryptUtils.getEncryptPassword(pwd);

        ReqRegisterBean bean = new ReqRegisterBean(username, secretPwd, mail, code);//密码需要加密
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.register(UrlHelper.REGISTER + UrlHelper.getUserName(), body).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<UserBean>(BaseApplication.getContext()) {
                    @Override
                    public void accept(UserBean loginBean) {
                        infoHint.registerSuccessInfo(loginBean);
                    }

                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.registerFailInfo(e.message);
                    }
                });

    }


    public void getCode(@NonNull final RegisterModel.ImageInfoHint infoHint) {
        httpService.getCodeImage().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                infoHint.getImageCodeSuccessInfo(response.body().byteStream());
                Log.e("下载图片", "成功" + response.body().byteStream());

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                ServerException e = ExceptionEngine.handleException(t);
                infoHint.getImageCodeFailInfo(e.getMessage());
            }
        });


    }


    //通过接口产生信息回调
    public interface RegisterInfoHint {
        //登录
        void registerSuccessInfo(UserBean loginBean);

        void registerFailInfo(String str);


    }

    public interface ImageInfoHint {
        //图片验证码
        void getImageCodeSuccessInfo(InputStream inputStream);

        void getImageCodeFailInfo(String str);

    }


}
